﻿using System;
using System.Linq;

namespace NC.Data
{
    public class AccountUserRepository
    {
        private readonly NEPALICORNEREntities _ncEntities;

        public AccountUserRepository()
        {
            _ncEntities = new NEPALICORNEREntities();
        }

        public AccountUser RegisterAccountUser(AccountUser accountUser)
        {
            _ncEntities.AccountUsers.Add(accountUser);
            _ncEntities.SaveChanges();
            return accountUser;
        }

        public AccountUser GetAccountUserByActivationCode(string activationCode)
        {
            return _ncEntities.AccountUsers.FirstOrDefault(x => x.ActivationCode == activationCode);
        }

        /// <summary>
        ///     To Check whether given email has been already registered or not
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool IsDuplicateEmail(string email)
        {
            var duplicateEmail = _ncEntities.AccountUsers.FirstOrDefault(x => x.Email == email);
            return duplicateEmail != null;
        }

        public bool ActivateUser(int accountUserId)
        {
            try
            {
                var dataAccountUser = _ncEntities.AccountUsers.FirstOrDefault(x => x.Id == accountUserId);
                if (dataAccountUser != null)
                    dataAccountUser.Active = true;
                _ncEntities.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public AccountUser GetAccountUserById(int id)
        {
            return _ncEntities.AccountUsers.FirstOrDefault(x => x.Id == id);
        }

        public bool SaveResetPasswordCode(string resetPasswordCode, int accountUserId)
        {
            try
            {
                var accountUser = _ncEntities.AccountUsers.SingleOrDefault(x => x.Id == accountUserId);
                if (accountUser != null)
                    accountUser.ResetPasswordCode = resetPasswordCode;
                else
                {
                    return false;
                }
                _ncEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public AccountUser GetAccountUserByResetPasswordCode(string resetPasswordCode)
        {
            return _ncEntities.AccountUsers.FirstOrDefault(x => x.ResetPasswordCode == resetPasswordCode);
        }

        public bool ResetPasswordByAccountUserId(string newPassword, int accountUserId)
        {
            try
            {
                var accountUser = _ncEntities.AccountUsers.SingleOrDefault(x => x.Id == accountUserId);
                if (accountUser == null) return false;
                accountUser.Password = newPassword;
                _ncEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///     To update accountUser Details with remaining information
        /// </summary>
        /// <param name="accountUser"></param>
        /// <returns></returns>
        public bool UpdateAccountUser(AccountUser accountUser)
        {
            try
            {
                var accountUserDetails = _ncEntities.AccountUsers.SingleOrDefault(x => x.Id == accountUser.Id);
                if (accountUserDetails != null)
                {
                    accountUserDetails.FirstName = accountUser.FirstName;
                    accountUserDetails.MiddleName = accountUser.MiddleName;
                    accountUserDetails.LastName = accountUser.LastName;
                    accountUserDetails.Sex = accountUser.Sex;
                    accountUserDetails.DOB = accountUser.DOB;
                    accountUserDetails.AddressId = accountUser.AddressId;
                }
                _ncEntities.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
